package com.wellsoft.mobileoa.utils.action;

import java.util.ArrayList;
import java.util.List;

import com.wellsoft.mobileoa.dbmodel.ContactInfo;
import com.wellsoft.mobileoa.utils.BaseAction;
import com.wellsoft.mobileoa.utils.BaseActivity;
import com.wellsoft.mobileoa.utils.BaseView;
import com.wellsoft.mobileoa.utils.BaseWidget;
import com.wellsoft.mobileoa.utils.SelectActionDataAdapter;
import com.wellsoft.mobileoa.utils.SelectContactListAdapter;
import com.wellsoft.mobileoa.utils.SystemConfig;
import com.wellsoft.mobileoa.wsmodel.WSOA_Action;
import com.wellsoft.mobileoa.wsmodel.WSOA_ActionData;
import com.wmobile.utils.PopListDialog;
import com.wmobile.utils.ServiceResponse;

public class PopSingleSelectAction extends BaseAction {

	public PopSingleSelectAction(WSOA_Action action, BaseView baseView) {
		this.action = action;
		this.baseView = baseView;
	}

	@Override
	public ServiceResponse excuteAction() {
		PopListDialog popDialog;
		List<Integer> selectedIndex;
		BaseWidget sender;
		BaseWidget hiddenSender;
		String senderSeparator = "";
		String hiddenSenderSeparator = "";
		String resultValue = "";
		String hiddenResultValue = "";
		if (SystemConfig.stringIsNullOrEmpty(action.getActionDataSet()
				.getDataSetType())) {
			selectedIndex = new ArrayList<Integer>();
			sender = baseView.getWidgetById(action.getPname());
			hiddenSender = baseView.getWidgetById(action.getPhname());
			String initValue = sender.getRawValue();
			
			senderSeparator = sender.getField().getSeparator();
			if (hiddenSender != null) {
				hiddenSenderSeparator = hiddenSender.getField().getSeparator();
			}
			if (!SystemConfig.stringIsNullOrEmpty(senderSeparator)
					&& senderSeparator.toUpperCase().equals("LINE")) {
				senderSeparator = "\n";
			}
			if (!SystemConfig.stringIsNullOrEmpty(hiddenSenderSeparator)
					&& hiddenSenderSeparator.toUpperCase().equals("LINE")) {
				hiddenSenderSeparator = "\n";
			}

			if (!SystemConfig.stringIsNullOrEmpty(initValue)) {
				int index = findIndexInActionData(initValue, action
						.getActionDataSet().getActionDatas());
				if (index > -1) {
					selectedIndex.add(Integer.valueOf(index));
				}
			}
			SelectActionDataAdapter dataAdapter = new SelectActionDataAdapter(
					baseView.getBaseActivity(), action.getActionDataSet()
							.getActionDatas(), selectedIndex, false);
			popDialog = new PopListDialog<WSOA_ActionData>(
					baseView.getBaseActivity(), action.getPageTitle()
							.getMessage(), selectedIndex, dataAdapter, false);
			if (popDialog.showDialog() != PopListDialog.DialogResultOK) {
				return new ServiceResponse(false);
			}
			selectedIndex = popDialog.getSelectedIndex();
			for (Integer i : selectedIndex) {
				if (!SystemConfig.stringIsNullOrEmpty(senderSeparator)) {
					resultValue += action.getActionDataSet().getActionDatas()
							.get(i.intValue()).getValue()
							+ senderSeparator;
				} else {
					resultValue += action.getActionDataSet().getActionDatas()
							.get(i.intValue()).getValue();
				}
				if (hiddenSender != null) {
					if (!SystemConfig.stringIsNullOrEmpty(hiddenSenderSeparator)) {
						hiddenResultValue += action.getActionDataSet()
								.getActionDatas().get(i.intValue())
								.getHiddenValue()
								+ hiddenSenderSeparator;
					} else if (SystemConfig.stringIsNullOrEmpty(hiddenSenderSeparator)) {
						hiddenResultValue += action.getActionDataSet()
								.getActionDatas().get(i.intValue())
								.getHiddenValue();
					}
				}
			}
			if (!SystemConfig.stringIsNullOrEmpty(resultValue)
					&& !SystemConfig.stringIsNullOrEmpty(senderSeparator)) {
				resultValue = resultValue.substring(0, resultValue.length()
						-senderSeparator.length());
			}
			if (hiddenSender != null
					&& !SystemConfig.stringIsNullOrEmpty(hiddenResultValue)
					&& !SystemConfig.stringIsNullOrEmpty(hiddenSenderSeparator)) {
				hiddenResultValue = hiddenResultValue.substring(0,
						hiddenResultValue.length()
								- hiddenSenderSeparator
										.length());
			}
			sender.setValue(resultValue);
			if (hiddenSender != null) {
				hiddenSender.setValue(hiddenResultValue);
			}
		} else if (action.getActionDataSet().getDataSetType().equals("unit")) {
			selectedIndex = new ArrayList<Integer>();
			sender = baseView.getWidgetById(action.getPname());
			String initValue = sender.getRawValue();
			
			senderSeparator = sender.getField().getSeparator();
			if (!SystemConfig.stringIsNullOrEmpty(senderSeparator)
					&& senderSeparator.toUpperCase().equals("LINE")) {
				senderSeparator = "\n";
			}

			if (!SystemConfig.stringIsNullOrEmpty(initValue)
					&& !SystemConfig.stringIsNullOrEmpty(senderSeparator)) {
				String[] strArr = initValue.split(senderSeparator);
				for (String str : strArr) {
					int index = findIndexInContactInfo(str,
							SystemConfig.getContactList());
					if (index > -1) {
						selectedIndex.add(Integer.valueOf(index));
					}
				}
			}
			SelectContactListAdapter dataAdapter = new SelectContactListAdapter(
					baseView.getBaseActivity(), SystemConfig.getContactList(),
					selectedIndex, false);
			popDialog = new PopListDialog<ContactInfo>(
					baseView.getBaseActivity(), action.getPageTitle()
							.getMessage(), selectedIndex, dataAdapter, false);
			if (popDialog.showDialog() != PopListDialog.DialogResultOK) {
				return new ServiceResponse(false);
			}
			selectedIndex = popDialog.getSelectedIndex();
			for (Integer i : selectedIndex) {
				if (!SystemConfig.stringIsNullOrEmpty(senderSeparator)) {
					resultValue += SystemConfig.getContactList()
							.get(i.intValue()).getName()
							+ senderSeparator;
				} else {
					resultValue += SystemConfig.getContactList()
							.get(i.intValue()).getName();
				}
			}
			if (!SystemConfig.stringIsNullOrEmpty(resultValue)) {
				if (!SystemConfig.stringIsNullOrEmpty(senderSeparator)) {
					resultValue = resultValue.substring(0, resultValue.length()
							- senderSeparator.length());
				}
			}
			sender.setValue(resultValue);
		}

		return new ServiceResponse(true);
	}

	private int findIndexInContactInfo(String value, List<ContactInfo> dataList) {
		if (SystemConfig.stringIsNullOrEmpty(value))
			return -1;
		for (int i = 0; i < dataList.size(); i++) {
			if (dataList.get(i).getName().equals(value))
				return i;
		}
		return -1;
	}

	private int findIndexInActionData(String value,
			List<WSOA_ActionData> dataList) {
		if (SystemConfig.stringIsNullOrEmpty(value))
			return -1;
		for (int i = 0; i < dataList.size(); i++) {
			if (dataList.get(i).getValue().equals(value))
				return i;
		}
		return -1;
	}
}
